<template>
	<div class="app-container">
		<!-- 搜索 -->
		<div class="filter-container tw-filter">
			<el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDelete()">批量删除</el-button>

      <el-input v-model="queryList.company_name" placeholder="请输入反馈公司" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.member_id" placeholder="请输入用户ID" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.member_name" placeholder="请输入用户名" class="filter-item tw-filter-input" clearable />
      <el-date-picker @change="changeData" value-format="yyyy-MM-dd" v-model="add_time_scope" type="daterange" unlink-panels :picker-options="pickerOptions"
      start-placeholder="开始日期" end-placeholder="结束日期" class="filter-item tw-filter-input-mr" clearable></el-date-picker>

			<el-button v-waves class="filter-item tw-filter-button" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
		</div>
    <el-dialog title="处理反馈" :visible.sync="dialogFormVisibleAudit" width="30%">
      <el-form label-width="80px">
        <el-form-item label="反馈内容">
          <el-input
            v-model="auditFeedback"
            type="textarea"
            :rows="4"
            placeholder="请输入处理反馈内容"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAudit = false">取消</el-button>
        <el-button type="primary" @click="submitAudit">提交</el-button>
      </div>
    </el-dialog>
		<!-- 表格内容 -->
		<el-table
		  v-loading="listLoading"
		  :data="tableList"
		  @selection-change="handleSelectionChange"
		  border fit highlight-current-row style="width: 100%;"
		>
			<el-table-column type="selection" width="50" />
      <el-table-column label="反馈公司" align="left" width="200">
        <template slot-scope="{row}">
          <span>{{row.company_name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="企业联系人" align="left" width="200">
        <template slot-scope="{row}">
          <span>{{row.company_mobile}}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="left" width="200">
        <template slot-scope="{row}">
          <span>{{row.mobile}}</span>
        </template>
      </el-table-column>
      <el-table-column label="希望处理的部门" align="left" width="200">
        <template slot-scope="{row}">
          <span>{{row.department}}</span>
        </template>
      </el-table-column>

      <el-table-column label="标题" prop="title" align="left" width="250" />
      <el-table-column label="内容" prop="details" align="left" width="250" />
      <el-table-column label="图片" prop="image_album" align="center">
      	<template slot-scope="{row}">
      		<el-image style="width: 60px; height: 60px;margin-right:5px;" fit="cover" v-for="(item, index) in row.image_album_arr" :src="item" :preview-src-list="[item]"></el-image>
      	</template>
      </el-table-column>
      <el-table-column label="反馈用户" align="left" width="220">
        <template slot-scope="{row}">
          <span>{{row.member_name}}【ID:{{row.member_id}}】</span>
        </template>
      </el-table-column>

      <el-table-column label="反馈时间" align="center" width="160">
        <template slot-scope="{row}">
          <span>{{ row.add_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center">
        <template slot-scope="{row}">
          <span v-if="row.status == 1">待处理</span>
          <span v-else-if="row.status == 2">已处理</span>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" prop="audit_remark" align="left" width="200" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
				<template slot-scope="{row}">
					<el-button size="mini"  @click="handleDelete(row.id)">删除</el-button>
          <el-button size="mini" type="success" @click="handleAudit(row)" v-if="row.status == 1">处理</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />
	</div>
</template>

<script>
	import Pagination from '@/components/Pagination' // 基于el分页的二次封装
	import waves from '@/directive/waves' // waves directive

	export default {
		components: { Pagination },
		directives: { waves },
		data() {
			return {
        // 数据列表
        listLoading: true,
        listTotal: 0,
        isPage: false,
        tableList: [],
        multipleSelection: [],//多选数据

        //键字段
        keyName: 'id',
        apiPath: 'member.feedback',

        // 搜索数据
        queryList: {
        	page: this.$store.state.settings.page, // 每页显示数量
        	curpage: 1, // 当前页码
        	company_name: '',
        	member_id: '',
        	member_name: '',
        	type: null,
					add_time_scope:'', //时间范围
        },
        add_time_scope: [],

        //带快捷选项时间
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        dialogFormVisibleAudit: false,
        auditFeedback: '已处理',
        selectedRow: null, // 存储当前处理的行数据

			}
		},
		created() {
			//获取数据列表
			this.getList()
		},
		methods: {
      //选择搜索时间范围
      changeData(val) {
      	this.queryList.add_time_scope = val[0] + ' - ' + val[1];
      },
/*******************************公共方法******************************/
      // 搜索
      handleFilter() {
      	this.queryList.curpage = 1
      	this.tableList = []
        this.listTotal = 0
      	this.getList()
      },
      // 列表选择
      handleSelectionChange(val) {
      	this.multipleSelection = val
      },
      // 重置form
      resetForm() {
      	this.form = JSON.parse(JSON.stringify(this.formReset))
      },
			// 获取数据列表
			getList() {
				this.listLoading = true
				this.$api.fetchList(this.queryList, this.apiPath).then(response => {
					this.tableList = response.list
					this.listTotal = response.list_count
					this.isPage = response.is_page
          this.listLoading = false
				})
			},
      // 删除
      handleDelete(id = 0) {
        let current = this
        let id_str = ''
        if (!id) {
          // 多选
          if (!this.multipleSelection.length) {
            this.$message({
              message: '请选择删除的条目！',
              type: 'warning'
            })
            return false
          }
          // 数据处理
          this.multipleSelection.forEach(item => {
            id_str += item[current.keyName] + ','
          })
        } else {
          id_str = id
        }

        // 提交数据
        this.$confirm('确认删除数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.del({ id_str: id_str }, this.apiPath).then(response => {
            // 重新获取列表
            this.getList()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          })
        })
      },

      // 删除
      handleAudit(row) {
        this.selectedRow = row; // 存储当前行数据
        this.dialogFormVisibleAudit = true; // 显示弹窗
      },
      submitAudit() {
        if (!this.auditFeedback.trim()) {
          this.$message.error('请输入处理反馈内容！');
          return;
        }

        this.$confirm('确定处理通过吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.$api.edit(
            { status: 2, id: this.selectedRow.id, audit_remark: this.auditFeedback },
            this.apiPath
          ).then(() => {
            this.getList(); // 刷新列表
            this.$message({ type: 'success', message: '处理成功!' });
          }).finally(() => {
            this.dialogFormVisibleAudit = false; // 关闭弹窗
          });
        });
      },
/*****************************公共方法END****************************/

		}
	}
</script>
